//
//  CCViewController.m
//  ccZone
//
//  Created by 陈小娟 on 2017/12/13.
//  Copyright © 2017年 CC. All rights reserved.
//

#import "CCViewController.h"

@interface CCViewController ()

@end

@implementation CCViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    if ([UIScreen mainScreen].bounds.size.height > 811) {
        [self showAsiPoneX];
    }else{
        [self showAsiPhoneNormal];
    }
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    if ([self isKindOfClass:NSClassFromString(@"youfillit1")]||
        [self isKindOfClass:NSClassFromString(@"youfillit2")]) {
        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    }else{
        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    }
    
    if ([self isKindOfClass:NSClassFromString(@"CCHomeController")]||
        [self isKindOfClass:NSClassFromString(@"CCMineController")]) {
        CCNavigationController * nav = (CCNavigationController *)self.navigationController;
        [nav resetPointerArray];
    }
}

-(void)showAsiPoneX {
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeAll;
    
    //1.contentView
    if ([self.view subviews].count > 0) {
        self.contentView = [self.view viewWithTag:1111];
    }else{
        UIView *contentview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, PORTRAIT_MAIN_SCREEN_W, PORTRAIT_MAIN_SCREEN_H-34)];
        self.contentView = contentview;
    }
    self.contentView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.contentView];
    
    //2.navbar
    UINavigationBar * kwNavBar = [[UINavigationBar alloc] initWithFrame:CGRectZero];
    _kwNavBar = kwNavBar;
    kwNavBar.translucent = NO;
    [self.contentView addSubview:kwNavBar];
    [kwNavBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(44.0f));
        make.left.equalTo(@(0.f));
        make.right.equalTo(@(0.f));
        make.height.equalTo(@(44.f));
    }];
    _ku_navigationItem = [[UINavigationItem alloc] initWithTitle:self.title];
    if ([self.navigationController.viewControllers indexOfObject:self]>0) {
        [self addBackButton];
    }
    _kwNavBar.items = @[_ku_navigationItem];
    UIView * devide = [[UIView alloc]initWithFrame:CGRectZero];
    devide.backgroundColor = TY_DEVIDE;
    [_kwNavBar addSubview:devide];
    [devide mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(43.5f));
        make.left.equalTo(@(0.f));
        make.right.equalTo(@(0.f));
        make.height.equalTo(@(0.5f));
    }];

    
    //3.statusBar
    UIView * zcStatusBar = [[UIView alloc] initWithFrame:CGRectZero];
    _kwStatusBar = zcStatusBar;
    _kwStatusBar.backgroundColor = TY_NAV;
    [self.contentView addSubview:_kwStatusBar];
    [_kwStatusBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@0.f);
        make.left.equalTo(@0.f);
        make.right.equalTo(@(0.f));
        make.height.equalTo(@(20.f));
    }];
}

-(void)showAsiPhoneNormal {
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeAll;
    
    //1.contentview
    if ([self.view subviews].count > 0) {       //awakeFromNib
        _contentView = [self.view viewWithTag:1111];
    }else{                                      //nomal
        //1.contectView
        UIView * contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, PORTRAIT_MAIN_SCREEN_W, PORTRAIT_MAIN_SCREEN_H)];
        _contentView = contentView;
        _contentView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_contentView];
        [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.bottom.equalTo(self.view);
        }];
    }
    
    //2.navbar
    UINavigationBar * kwNavBar = [[UINavigationBar alloc] initWithFrame:CGRectZero];
    _kwNavBar = kwNavBar;
    kwNavBar.translucent = NO;
    [self.contentView addSubview:kwNavBar];
    [kwNavBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(20.f));
        make.left.equalTo(@(0.f));
        make.right.equalTo(@(0.f));
        make.height.equalTo(@(44.f));
    }];
    
    _ku_navigationItem = [[UINavigationItem alloc] initWithTitle:self.title];
    if ([self.navigationController.viewControllers indexOfObject:self]>0) {
        [self addBackButton];
    }
    _kwNavBar.items = @[_ku_navigationItem];
    UIView * devide = [[UIView alloc]initWithFrame:CGRectZero];
    devide.backgroundColor = TY_DEVIDE;
    [_kwNavBar addSubview:devide];
    [devide mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(43.5f));
        make.left.equalTo(@(0.f));
        make.right.equalTo(@(0.f));
        make.height.equalTo(@(0.5f));
    }];
    //2.statusBar
    UIView * zcStatusBar = [[UIView alloc] initWithFrame:CGRectZero];
    _kwStatusBar = zcStatusBar;
    _kwStatusBar.backgroundColor = TY_NAV;
    [self.contentView addSubview:_kwStatusBar];
    [_kwStatusBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@0.f);
        make.left.equalTo(@0.f);
        make.right.equalTo(@(0.f));
        make.height.equalTo(@(20.f));
    }];
}

-(void)navBack {
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)addBackButton {
    if (@available(iOS 11.0, *)) {
        UIButton* btnBack = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
        [btnBack setImage:[UIImage imageNamed:@"nav_back_icon"] forState:UIControlStateNormal];
        [btnBack addTarget:self action:@selector(navBack) forControlEvents:UIControlEventTouchUpInside];
        btnBack.imageEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, 0.0, 30.0);
        UIView * backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
        [backView addSubview:btnBack]; //将btnBack放在backView中，否则会出现btnBack响应区域过大的BUG
        btnBack.backgroundColor = [UIColor clearColor];
        _ku_navigationItem.leftBarButtonItems = @[[[UIBarButtonItem alloc] initWithCustomView:backView]];
    }else{
        UIBarButtonItem * negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
        [negativeSpacer setWidth:-22];
        UIButton* btnBack = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
        [btnBack setImage:[UIImage imageNamed:@"nav_back_icon"] forState:UIControlStateNormal];
        [btnBack addTarget:self action:@selector(navBack) forControlEvents:UIControlEventTouchUpInside];
        UIView * backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
        [backView addSubview:btnBack]; //将btnBack放在backView中，否则会出现btnBack响应区域过大的BUG
        _ku_navigationItem.leftBarButtonItems = @[negativeSpacer,[[UIBarButtonItem alloc] initWithCustomView:backView]];
    }
}

-(void)hideTop {

    [_kwNavBar mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(-5.f));
        make.left.equalTo(@0.f);
        make.right.equalTo(@(0.f));
        make.height.equalTo(@(5.f));
    }];
    

    [_kwStatusBar mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(-5.f));
        make.left.equalTo(@0.f);
        make.right.equalTo(@(0.f));
        make.height.equalTo(@(5.f));
    }];
    _kwStatusBar.hidden = YES;
    _kwNavBar.hidden = YES;
}

-(void)setTitle:(NSString *)title {
    [super setTitle:title];
    _ku_navigationItem.title = title;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotate {
    return YES;
}

-(void)dealloc {
    NSLog(@"%@--delloc",NSStringFromClass([self class]));
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
